package com.yaolisheng.poetryandode.config;

/**
 * @Author yls
 * @Date 2021/6/4 15:45
 * @Version 1.0
 */

import com.yaolisheng.poetryandode.entity.IUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@Component
@Slf4j
public class UserLoginInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                             Object handler)throws Exception {

        HttpSession session = request.getSession(true);
        IUser sessionUser = (IUser) session.getAttribute("sessionUser");
        if(null!=sessionUser) {//已登录
            return true;
        }else {//未登录
            //直接重定向到登录页面
            response.sendRedirect(request.getContextPath()+"/");
            return false;
        }
    }

}



